package com.app.biz.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.app.biz.domain.request.BizCityIndustrialEconomicsRequest;
import com.app.biz.domain.BizCityIndustrialEconomics;
import com.app.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizCityIndustrialEconomicsFormalMapper;
import com.app.biz.domain.formal.BizCityIndustrialEconomicsFormal;
import com.app.biz.service.IBizCityIndustrialEconomicsFormalService;

/**
 * 分市工业企业主要经济指标正式SService业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@Service
public class BizCityIndustrialEconomicsFormalServiceImpl implements IBizCityIndustrialEconomicsFormalService
{

    private BizCityIndustrialEconomicsFormalMapper bizCityIndustrialEconomicsFormalMapper;

    @Autowired
    public BizCityIndustrialEconomicsFormalServiceImpl(BizCityIndustrialEconomicsFormalMapper bizCityIndustrialEconomicsFormalMapper){
        this.bizCityIndustrialEconomicsFormalMapper = bizCityIndustrialEconomicsFormalMapper;
    }

    /**
     * 查询分市工业企业主要经济指标正式S
     *
     * @param baseId 分市工业企业主要经济指标正式S主键
     * @return 分市工业企业主要经济指标正式S
     */
    @Override
    public BizCityIndustrialEconomicsFormal selectBizCityIndustrialEconomicsFormalByBaseId(String baseId)
    {
        return bizCityIndustrialEconomicsFormalMapper.selectBizCityIndustrialEconomicsFormalByBaseId(baseId);
    }

    /**
     * 查询分市工业企业主要经济指标正式S列表
     *
     * @param bizCityIndustrialEconomicsFormal 分市工业企业主要经济指标正式S
     * @return 分市工业企业主要经济指标正式S
     */
    @Override
    public List<BizCityIndustrialEconomicsFormal> selectBizCityIndustrialEconomicsFormalList(BizCityIndustrialEconomicsFormal bizCityIndustrialEconomicsFormal)
    {
        return bizCityIndustrialEconomicsFormalMapper.selectBizCityIndustrialEconomicsFormalList(bizCityIndustrialEconomicsFormal);
    }

    /**
     * 新增分市工业企业主要经济指标正式S
     *
     * @param bizCityIndustrialEconomicsFormal 分市工业企业主要经济指标正式S
     * @return 结果
     */
    @Override
    public int insertBizCityIndustrialEconomicsFormal(BizCityIndustrialEconomicsFormal bizCityIndustrialEconomicsFormal)
    {
        bizCityIndustrialEconomicsFormal.setBaseId(IdUtil.simpleUUID());
        bizCityIndustrialEconomicsFormal.setBaseCreateTime(DateUtils.getNowDate());
        return bizCityIndustrialEconomicsFormalMapper.insertBizCityIndustrialEconomicsFormal(bizCityIndustrialEconomicsFormal);
    }

    /**
     * 修改分市工业企业主要经济指标正式S
     *
     * @param bizCityIndustrialEconomicsFormal 分市工业企业主要经济指标正式S
     * @return 结果
     */
    @Override
    public int updateBizCityIndustrialEconomicsFormal(BizCityIndustrialEconomicsFormal bizCityIndustrialEconomicsFormal)
    {
        return bizCityIndustrialEconomicsFormalMapper.updateBizCityIndustrialEconomicsFormal(bizCityIndustrialEconomicsFormal);
    }

    /**
     * 批量删除分市工业企业主要经济指标正式S
     *
     * @param baseIds 需要删除的分市工业企业主要经济指标正式S主键
     * @return 结果
     */
    @Override
    public int deleteBizCityIndustrialEconomicsFormalByBaseIds(String[] baseIds)
    {
        return bizCityIndustrialEconomicsFormalMapper.deleteBizCityIndustrialEconomicsFormalByBaseIds(baseIds);
    }

    /**
     * 删除分市工业企业主要经济指标正式S信息
     *
     * @param baseId 分市工业企业主要经济指标正式S主键
     * @return 结果
     */
    @Override
    public int deleteBizCityIndustrialEconomicsFormalByBaseId(String baseId)
    {
        return bizCityIndustrialEconomicsFormalMapper.deleteBizCityIndustrialEconomicsFormalByBaseId(baseId);
    }

    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizCityIndustrialEconomicsFormalMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            return uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * <p>
     * 通过日期查询表格中的数据
     * </p>
     *
     * @param request 前端传传来的数据
     * @return 表格中的数据
     */
    @Override
    public List<BizCityIndustrialEconomics> getEconomicIndicatorsByDateList(BizCityIndustrialEconomicsRequest request) {
        String dates = request.getDates();
        DateTime parse = DateUtil.parse(dates, "yyyy年MM月");
        request.setDates(DateUtil.format(parse,"yyyy-MM-dd"));
        return bizCityIndustrialEconomicsFormalMapper.getEconomicIndicatorsByDateList(request);
    }
}
